package com.stylefeng.guns.rest.modular.order;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.order.OrderServiceAPI;
import com.stylefeng.guns.api.order.vo.OrderVO;
import com.stylefeng.guns.rest.common.CurrentUser;
import com.stylefeng.guns.rest.modular.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order/")
public class OrderController {

    @Reference(interfaceClass = OrderServiceAPI.class,check = false)
    private OrderServiceAPI orderServiceAPI;

    /**
     * 购票
     * @param fieldId
     * @param soldSeats
     * @param seatsName
     * @return
     */
    @PostMapping("buyTickets")
    public ResponseVO buyTickets(Integer fieldId,String soldSeats,String seatsName){
        try {
// 验证售出的票是否为真
            boolean trueSeats = orderServiceAPI.isTrueSeats(fieldId + "", soldSeats);

            // 验证已经售卖的订单，有没有这些座位
            boolean notSoldSeats = orderServiceAPI.isNotSoldSeats(fieldId + "", soldSeats);

            // 验证上述两个条件有一个不为真，则不创建订单信息
            if (trueSeats && notSoldSeats){
                // 创建订单 (注意获取当前登录人)
                String userId = CurrentUser.getCurrentUser();
                if (userId == null || userId.trim().length() <= 0){
                    return ResponseVO.serviceFail("用户未登录！");
                }
                OrderVO orderVO = orderServiceAPI.saveOrderInfo(fieldId, soldSeats, seatsName, Integer.valueOf(userId));
                if (orderVO == null){
                    log.error("购票未成功");
                    return ResponseVO.serviceFail("购票未成功");
                }else {
                    return ResponseVO.success(orderVO);
                }
            }else {
                log.error("订单中的座位编号有问题！");
                return ResponseVO.serviceFail("订单中的座位编号有问题！");
            }
        }catch (Exception e){
            log.error("购票业务异常：{}",e.getMessage());
            return ResponseVO.serviceFail("购票业务异常");
        }
    }

    /**
     * 获取订单信息
     * @param nowPage
     * @param pageSize
     * @return
     */
    @PostMapping("getOrderInfo")
    public ResponseVO getOrderInfo(
            @RequestParam(name = "nowPage",required = false,defaultValue = "1") Integer nowPage,
            @RequestParam(name = "pageSize",required = false,defaultValue = "5") Integer pageSize
    ){
        // 获取当前登陆人的信息
        String userId = CurrentUser.getCurrentUser();
        // 使用当前登录人获取已经购买的订单
        Page<OrderVO> page = new Page<>(nowPage,pageSize);
        if (userId != null && userId.trim().length() > 0){
            Page<OrderVO> result = orderServiceAPI.getOrderByUserId(Integer.valueOf(userId),page);
            return ResponseVO.success(nowPage,(int) result.getTotal(),"",result);
        }
        return ResponseVO.serviceFail("用户未登录！");
    }
}
